﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Core
{
    public delegate void BlockDestroyedHandler(object sender, EventArgs e);
    public delegate void BlockPlacedHandler(object sender, EventArgs e);

    /// <summary>
    /// Represents a block with propertys(ID, Color, Vertices, Indices, ...)
    /// </summary>
    /// <param name="_position"></param>
    /// <param name="_color"></param>
    /// <param name="_id"></param>
    public abstract class Block
    {
        static Block()
        {
            blocks = new Dictionary<byte, Block>();
        }
        public Block(Vector3 _position, Color _color, byte _id)
        {
            if (blocks.ContainsKey(_id))
            {
                Debug.LogWarning(String.Format("Block ID: {0} exist twice = cant register block", id));
                return;
            }

            blocks.Add(_id, this);

            position = _position;
            id = _id;
            color = _color;
            destroyable = true;
        }

        private static Dictionary<byte, Block> blocks;

        private Vector3 position;

        protected byte id;
        protected Color color;
        protected bool destroyable;

        public event BlockDestroyedHandler BlockDestroyed;
        public event BlockPlacedHandler BlockPlaced;

        public static Dictionary<byte, Block> BlockIDs
        {
            get
            {
                return blocks;
            }
        }

        public void CreateBlock()
        {
            this.BlockPlaced(this, new EventArgs());
        }

        public void DestroyBlock()
        {
            this.BlockDestroyed(this, new EventArgs());
        }
    }
}
